package cn.lzd.mr.count.writable;

import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * 学习：先了解、后深入；先记录、后记忆；先理论、后实践；先模仿、后创新
 * Created by lzd on 2018/5/14.
 */
public class WriteBean implements Writable {

    //反序列化需要反射调用空参构造，必须有
    public WriteBean() {
    }

    private String str;

    private Integer strLength;

    public String getStr() {
        return str;
    }

    public Integer getStrLength() {
        return strLength;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public void setStrLength(Integer strLength) {
        this.strLength = strLength;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(str);
        dataOutput.writeInt(strLength);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.str = dataInput.readUTF();
        this.strLength = dataInput.readInt();
    }

    //想写入文件必须重写tostring
    @Override
    public String toString() {
        return str + "\t" + strLength;

    }
}
